package dS.ProjectGPS.LeicaOWI;

import javax.microedition.lcdui.*;

public class FormOwiOCC extends Form implements FormOwi {

  OWIAgent owiAgent = null;

  TextField pointId = new TextField("Point Identifier", "", 16, TextField.ANY);
  ChoiceGroup posFormat = new ChoiceGroup("Position Format", ChoiceGroup.POPUP);
  StringItem note1 = new StringItem("", "If WGS84 Geodetic:", StringItem.PLAIN);
  TextField wLat = new TextField("WGS84 Latitude", "", 50, TextField.DECIMAL);
  TextField wLon = new TextField("WGS84 Longitude", "", 50, TextField.DECIMAL);
  TextField wHei = new TextField("WGS84 Height (m)", "", 50, TextField.DECIMAL);
  StringItem note2 = new StringItem("", "If Local Grid:", StringItem.PLAIN);
  TextField gNorthing = new TextField("Grid Northing (m)", "", 50,
                                      TextField.DECIMAL);
  TextField gEasting = new TextField("Grid Easting (m)", "", 50,
                                     TextField.DECIMAL);
  TextField heightOfMarker = new TextField("Height of Marker (m)", "", 50,
                                           TextField.DECIMAL);

  TextField kpcq = new TextField("Known Point Coordinate Quality (m)", "", 50,
                                 TextField.DECIMAL);
  TextField antHeight = new TextField("Antenna Height", "", 50,
                                      TextField.DECIMAL);
  ChoiceGroup antType = new ChoiceGroup("Antenna Type", ChoiceGroup.POPUP);
  TextField occTime = new TextField("Occupation Time (s)", "", 50,
                                    TextField.DECIMAL);
  ChoiceGroup autoEnd = new ChoiceGroup("Automatic End", ChoiceGroup.POPUP);

  public FormOwiOCC(OWIAgent o) {
    super("Occupy Known Point");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    append(pointId);
    append(kpcq);
    append(antHeight);
    antType.append("Vertical", null);
    antType.append("Slope", null);
    append(antType);
    append(occTime);
    autoEnd.append("No", null);
    autoEnd.append("Yes", null);
    append(autoEnd);
    append(posFormat);
    posFormat.append("WGS84 Geodetic", null);
    posFormat.append("Local Grid", null);
    append(note1);
    append(wLat);
    append(wLon);
    append(wHei);
    append(note2);
    append(gNorthing);
    append(gEasting);
    append(heightOfMarker);
  }

  public void update() {
    // N/A
  }

  public void save() {

    String msgId = "OCC";

    // Setup SET parameters
    String[] outputs;

    if (posFormat.getSelectedIndex() == 0) {
      String[] temp = {
                    msgId, pointId.getString(), "" + posFormat.getSelectedIndex(),
                    wLat.getString(), wLon.getString(), wHei.getString(), kpcq.getString(),
                    antHeight.getString(), "" + antType.getSelectedIndex(),
                    occTime.getString(), "" + autoEnd.getSelectedIndex()
      };
      outputs = temp;
    } else {
      String[] temp = {
              msgId, pointId.getString(), "" + posFormat.getSelectedIndex(),
              gNorthing.getString(), gEasting.getString(),
              heightOfMarker.getString(), kpcq.getString(),
              antHeight.getString(), "" + antType.getSelectedIndex(),
              occTime.getString(), "" + autoEnd.getSelectedIndex()
      };
      outputs = temp;

    }
    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputs)));
    Controller.showACKAlert(this, this, ackCode);
  }

}
